package com.zyp.messageboard.service.impl;

import com.zyp.messageboard.dao.MessageDao;
import com.zyp.messageboard.dao.impl.MessageDaoImpl;
import com.zyp.messageboard.entity.Message;
import com.zyp.messageboard.service.MessageService;

import java.util.List;

public class MessageServiceImpl implements MessageService {

    private final MessageDao messageDao = new MessageDaoImpl();

    @Override
    public Message findById(Integer id) {
        Message message = messageDao.findById(id);
        if (message != null) {
            message.setFormattedCreatedAt(message.getFormattedCreatedAt());
        }
        return message;
    }

    @Override
    public List<Message> findByUserId(Integer userId) {
        List<Message> messages = messageDao.findByUserId(userId);
        for (Message message : messages) {
            message.setFormattedCreatedAt(message.getFormattedCreatedAt());
        }
        return messages;
    }

    @Override
    public List<Message> findAll() {
        List<Message> messages = messageDao.findAll();
        for (Message message : messages) {
            message.setFormattedCreatedAt(message.getFormattedCreatedAt());
        }
        return messages;
    }

    @Override
    public void save(Message message) {
        messageDao.save(message);
    }

    @Override
    public void update(Message message) {
        messageDao.update(message);
    }

    @Override
    public void delete(Integer id) {
        messageDao.delete(id);
    }
}
